package org.dtrd.modules.evaluation.service.impl;

import com.baomidou.dynamic.datasource.annotation.DS;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.apache.commons.lang3.ArrayUtils;
import org.dtrd.modules.evaluation.entity.bean.DiseaseEvaluationInfo;
import org.dtrd.modules.evaluation.entity.dto.DiseaseDTO;
import org.dtrd.modules.evaluation.entity.po.DtrdEntEvaluationRdDiabeticFoot;
import org.dtrd.modules.evaluation.mapper.DtrdEntEvaluationRdDiabeticFootMapper;
import org.dtrd.modules.evaluation.service.IDtrdEntEvaluationRdDiabeticFootService;
import org.springframework.stereotype.Service;

import java.util.*;

/**
 * <p>
 * 下肢血管病变及足病评估
 服务实现类
 * </p>
 *
 * @author QR
 * @since 2021-09-26
 */
@Service
@DS("multi-datasource1")
public class DtrdEntEvaluationRdDiabeticFootServiceImpl extends ServiceImpl<DtrdEntEvaluationRdDiabeticFootMapper, DtrdEntEvaluationRdDiabeticFoot> implements IDtrdEntEvaluationRdDiabeticFootService {

    @Override
    public DiseaseEvaluationInfo saveDiabeticFootInfo(DiseaseDTO dto) {
        // 分类或分级诊断
        Map<Integer, String> diagnoseResultMap = new HashMap<>();
        // 建议复诊时间
        Map<Integer, String> revisitTimeMap = new HashMap<>();
        revisitTimeMap.put(1, "请及时就诊");
        revisitTimeMap.put(2, "常规进行下肢动脉粥样硬化病变筛查");
        revisitTimeMap.put(3, "且以后至少1年1次随诊");
        revisitTimeMap.put(4, "建议1年复诊1次");
        revisitTimeMap.put(5, "建议6个月复诊1次");
        revisitTimeMap.put(6, "每3-6个月复查1次");
        // 就诊科室
        Map<Integer, String> departmentMap = new HashMap<>();
        departmentMap.put(1, "内分泌科就诊");
        departmentMap.put(2, "必要时骨科及血管外科联合就诊");
        // 具体检查内容推荐
        Map<Integer, String> examinationProjectMap = new HashMap<>();
        examinationProjectMap.put(1, "1、有以下体格检查：<br>" + "    1）皮肤温度测定<br>" + "    2）间歇性跛行试验<br>" + "    3）足背/胫后动脉检查<br>" + "    4）颈动脉听诊<br>"
                + "    5）股动脉听诊<br>" + "2、器械检查：<br>" + "    1）ABI<br>" + "    2）下肢血管超声<br>" + "    3）CTA/MRI（必要时）");
        examinationProjectMap.put(2,
                "1、足部视诊：<br>" + "2、有以下体格检查：<br>" + "    1）皮肤温度测定<br>" + "    2）间歇性跛行试验<br>" + "    3）足背/胫后动脉检查<br>" + "    4）颈动脉听诊<br>" + "    5）股动脉听诊<br>"
                        + "    6）压力觉、振动觉、踝反射等<br>" + "3、器械检查：<br>" + "    1）ABI<br>" + "    2）下肢血管超声<br>" + "    3）CTA/MRI（必要时）<br>" + "    4）下肢肌电图");
        // 是否确诊：1-未确诊,2-已确诊,3-确诊无
        int isDiagnose = dto.getIsDiagnose();
        // 症状或检查
        Integer[] symptomArr = dto.getSymptom();
        StringJoiner joiner = new StringJoiner(",");
        for (Integer i : symptomArr) {
            joiner.add(i+"");
        }
        Set<Integer> diagnoseResultSet = new HashSet<>();
        Set<Integer> revisitTimeSet = new HashSet<>();
        Set<Integer> departmentSet = new HashSet<>();
        Set<Integer> examinationProjectSet = new HashSet<>();
        if (1 == isDiagnose) {
            if (ArrayUtils.contains(symptomArr, 1)) {
                revisitTimeSet.add(2);
                departmentSet.add(1);
                examinationProjectSet.add(1);
            }
            if (ArrayUtils.contains(symptomArr, 2) || ArrayUtils.contains(symptomArr, 3) || ArrayUtils.contains(symptomArr, 4)
                    || ArrayUtils.contains(symptomArr, 5) || ArrayUtils.contains(symptomArr, 6)) {
                revisitTimeSet.add(2);
                revisitTimeSet.add(3);
                departmentSet.add(1);
                examinationProjectSet.add(1);
            }
            if (ArrayUtils.contains(symptomArr, 7) || ArrayUtils.contains(symptomArr, 8) || ArrayUtils.contains(symptomArr, 9)
                    || ArrayUtils.contains(symptomArr, 10)) {
                revisitTimeSet.add(1);
                departmentSet.add(1);
                examinationProjectSet.add(1);
            }
        } else if (2 == isDiagnose) {
            if (ArrayUtils.contains(symptomArr, 1) || ArrayUtils.contains(symptomArr, 2)) {
                revisitTimeSet.add(6);
                departmentSet.add(1);
                examinationProjectSet.add(2);
            }
            if (ArrayUtils.contains(symptomArr, 1) && ArrayUtils.contains(symptomArr, 3)) {
                revisitTimeSet.add(6);
                departmentSet.add(1);
                departmentSet.add(2);
                examinationProjectSet.add(2);
            }
        } else if (3 == isDiagnose) {
            if (ArrayUtils.contains(symptomArr, 1)) {
                revisitTimeSet.add(4);
                departmentSet.add(1);
                examinationProjectSet.add(1);
            } else if (ArrayUtils.contains(symptomArr, 2)) {
                revisitTimeSet.add(5);
                departmentSet.add(1);
                examinationProjectSet.add(2);
            }
        }
        StringBuilder diagnoseResult = new StringBuilder();
        StringBuilder revisitTime = new StringBuilder();
        StringBuilder department = new StringBuilder();
        StringBuilder examinationProject = new StringBuilder();
        for (int diagnoseResultKey : diagnoseResultSet) {
            diagnoseResult.append(diagnoseResultMap.get(diagnoseResultKey)).append("、");
        }
        diagnoseResult = new StringBuilder("".equals(diagnoseResult.toString()) ? "" : diagnoseResult.substring(0, diagnoseResult.length() - 1));
        for (int revisitTimeKey : revisitTimeSet) {
            revisitTime.append(revisitTimeMap.get(revisitTimeKey)).append("，");
        }
        revisitTime = new StringBuilder("".equals(revisitTime.toString()) ? "" : revisitTime.substring(0, revisitTime.length() - 1));
        for (int departmentKey : departmentSet) {
            department.append(departmentMap.get(departmentKey)).append("，");
        }
        department = new StringBuilder("".equals(department.toString()) ? "" : department.substring(0, department.length() - 1));
        // int examinationProjectIndex = 0;
        for (int examinationProjectKey : examinationProjectSet) {
            // examinationProject += (++examinationProjectIndex) + "、" +
            // examinationProjectMap.get(examinationProjectKey) + "\n";
            examinationProject.append(examinationProjectMap.get(examinationProjectKey)).append("<br>");
        }
        examinationProject = new StringBuilder("".equals(examinationProject.toString()) ? "" : examinationProject.substring(0, examinationProject.length() - 4));
        DtrdEntEvaluationRdDiabeticFoot diabeticFoot = new DtrdEntEvaluationRdDiabeticFoot();
        diabeticFoot.setIsDiagnose(isDiagnose);
        diabeticFoot.setSymptom(joiner.toString());
        diabeticFoot.setDiagnoseResult(diagnoseResult.toString());
        diabeticFoot.setRevisitTime(revisitTime.toString());
        diabeticFoot.setDepartment(department.toString());
        diabeticFoot.setExaminationProject(examinationProject.toString());
        diabeticFoot.setOutput(diagnoseResult + "<br>" + revisitTime + "<br>" + department + "<br>" + examinationProject);
        // 有用户id就存储数据库
        if (dto.getPatientId() != null) {
            diabeticFoot.setPatientId(dto.getPatientId());
            diabeticFoot.setUserType(1);
            baseMapper.insert(diabeticFoot);
        }
        return new DiseaseEvaluationInfo().parseFromPo(diabeticFoot);
    }

    @Override
    public DiseaseEvaluationInfo getDiabeticFootInfo(Integer patientId) {
        LambdaQueryWrapper<DtrdEntEvaluationRdDiabeticFoot> lambdaQueryWrapper = new LambdaQueryWrapper<>();
        lambdaQueryWrapper
                .eq(DtrdEntEvaluationRdDiabeticFoot::getPatientId, patientId)
                .eq(DtrdEntEvaluationRdDiabeticFoot::getIsDel, 0)
                .orderByDesc(DtrdEntEvaluationRdDiabeticFoot::getCreateTime)
                .last("limit 1");
        DtrdEntEvaluationRdDiabeticFoot diabeticFoot = baseMapper.selectOne(lambdaQueryWrapper);
        DiseaseEvaluationInfo info = new DiseaseEvaluationInfo();
        if (diabeticFoot != null) {
            info = info.parseFromPo(diabeticFoot);
        }
        return info;
    }
}
